package com.nkbh.sevenday.mykotlin.activity

import android.content.Context
import android.content.SharedPreferences
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.Toast
import com.nkbh.sevenday.mykotlin.R
import kotlinx.android.synthetic.main.activity_register.*
import kotlinx.android.synthetic.main.title.*

class RegisterActivity : AppCompatActivity(), View.OnClickListener {
    override fun onClick(v: View?) {
        when(v?.id){
            R.id.btnConfirm -> registerUser()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_register)

        btnBack.setOnClickListener { finish() }
        btnConfirm.setOnClickListener(this)

        tvTitle.text="注册"

    }

    fun registerUser(){

        //TODO 注册操作

        if (TextUtils.isEmpty(etUsername.text.toString().trim()) || TextUtils.isEmpty(etPwd.text.toString().trim())
        || TextUtils.isEmpty(etPwd1.text.toString().trim())){
            Toast.makeText(this,"不能为空",Toast.LENGTH_SHORT).show()
            return
        }

        if (!etPwd.text.toString().trim().equals(etPwd1.text.toString().trim())){
            Toast.makeText(this,"两次输入的密码不一致",Toast.LENGTH_SHORT).show()
            return
        }

        val prefs: SharedPreferences by lazy { this.getSharedPreferences("USER", Context.MODE_PRIVATE) }
        val edit = prefs.edit()
        edit.putString("username",etUsername.text.toString().trim())
        edit.putString("key",etPwd.text.toString().trim())
        edit.commit()
        finish()
    }
}
